﻿Imports Microsoft.Office.Tools.Ribbon
Imports Microsoft.Office.Interop.Word
Imports System.Windows.Forms
Imports Microsoft.Office.Core
Imports System.Drawing
Public Class Ribbon1
    Private prohibitedWordsPath As String = ""

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button1.Click
        ' 设置违禁词文件路径
        Dim openFileDialog As New OpenFileDialog()
        openFileDialog.Filter = "Text files (*.txt)|*.txt"
        openFileDialog.Title = "选择违禁词文件"

        If openFileDialog.ShowDialog() = DialogResult.OK Then
            prohibitedWordsPath = openFileDialog.FileName
            MessageBox.Show("违禁词文件路径已设置")
        End If
    End Sub

    Private Sub SplitButton1_Click(sender As Object, e As RibbonControlEventArgs) Handles SplitButton1.Click
        ' 查找违禁词并标记
        If String.IsNullOrEmpty(prohibitedWordsPath) Then
            MessageBox.Show("请先设置违禁词文件路径")
            Return
        End If

        Dim doc As Document = Globals.ThisAddIn.Application.ActiveDocument
        Dim prohibitedWords As String() = System.IO.File.ReadAllText(prohibitedWordsPath).Split(","c)

        ' 创建一个列表来存储所有违禁词的位置信息
        Dim results As New System.Text.StringBuilder()
        results.AppendLine("违禁词检查结果报告")
        results.AppendLine("==================")
        results.AppendLine($"检查时间: {DateTime.Now}")
        results.AppendLine($"文档名称: {doc.Name}")
        results.AppendLine()

        For Each X In prohibitedWords
            Dim range As Range = doc.Content
            range.Find.ClearFormatting()

            With range.Find
                .Text = X.Trim()
                .Forward = True
                .Wrap = WdFindWrap.wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = True
                .Execute()

                ' 查找该词的所有出现位置
                Do While .Found
                    range.HighlightColorIndex = WdColorIndex.wdYellow
                    Dim pageNum As Integer = range.Information(WdInformation.wdActiveEndPageNumber)
                    Dim lineNum As Integer = range.Information(WdInformation.wdFirstCharacterLineNumber)
                    results.AppendLine($"违禁词 ""{X}"" 出现在第 {pageNum} 页，第 {lineNum} 行")
                    .Execute()
                Loop
            End With
        Next

        ' 如果找到了违禁词，导出结果到文件
        If results.Length > 0 Then
            Dim saveFileDialog As New SaveFileDialog()
            saveFileDialog.Filter = "Text files (*.txt)|*.txt"
            saveFileDialog.Title = "选择保存检查结果的位置"
            saveFileDialog.FileName = $"违禁词检查结果_{DateTime.Now:yyyyMMdd_HHmmss}.txt"

            If saveFileDialog.ShowDialog() = DialogResult.OK Then
                System.IO.File.WriteAllText(saveFileDialog.FileName, results.ToString())
                MessageBox.Show($"检查完成！结果已保存到：{saveFileDialog.FileName}", "检查完成")
            End If
        Else
            MessageBox.Show("未找到任何违禁词", "检查完成")
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs)
        ' 降低图片饱和度
        Dim doc As Document = Globals.ThisAddIn.Application.ActiveDocument

        For Each shape In doc.InlineShapes
            If shape.Type = WdInlineShapeType.wdInlineShapePicture Then
                shape.PictureFormat.Brightness = 0.5
                ' 使用 ColorType 属性来实现黑白效果
                shape.PictureFormat.ColorType = MsoPictureColorType.msoPictureGrayscale
            End If
        Next

        For Each shape In doc.Shapes
            If shape.Type = MsoShapeType.msoPicture Then
                shape.PictureFormat.Brightness = 0.5
                ' 使用 ColorType 属性来实现黑白效果
                shape.PictureFormat.ColorType = MsoPictureColorType.msoPictureGrayscale
            End If
        Next

        MessageBox.Show("所有图片已调整为黑白效果")
    End Sub

    Private Sub Button3_Click(sender As Object, e As RibbonControlEventArgs) Handles Button3.Click
        ' 查找特定样式
        ' 创建样式选择对话框
        Dim styleForm As New Form()
        styleForm.Text = "选择要查找的样式"
        styleForm.Size = New Size(300, 150)
        styleForm.StartPosition = FormStartPosition.CenterScreen

        Dim styleComboBox As New ComboBox()
        styleComboBox.Location = New System.Drawing.Point(20, 20)
        styleComboBox.Size = New Size(240, 25)
        styleComboBox.DropDownStyle = ComboBoxStyle.DropDownList

        ' 添加常用样式
        Dim doc As Document = Globals.ThisAddIn.Application.ActiveDocument
        Dim commonStyles() As String = {"正文", "标题 1", "标题 2", "标题 3", "引用", "强调", "副标题"}
        For Each style In commonStyles
            Try
                If doc.Styles(style) IsNot Nothing Then
                    styleComboBox.Items.Add(style)
                End If
            Catch
                Continue For
            End Try
        Next

        ' 添加确定按钮
        Dim okButton As New Button()
        okButton.Text = "确定"
        okButton.DialogResult = DialogResult.OK
        okButton.Location = New System.Drawing.Point(100, 60)

        styleForm.Controls.Add(styleComboBox)
        styleForm.Controls.Add(okButton)

        If styleForm.ShowDialog() <> DialogResult.OK OrElse styleComboBox.SelectedItem Is Nothing Then Return

        Dim styleName As String = styleComboBox.SelectedItem.ToString()
        Dim range As Range = doc.Content
        range.Find.ClearFormatting()

        ' 创建保存文件对话框
        Dim saveFileDialog As New SaveFileDialog()
        saveFileDialog.Filter = "Text files (*.txt)|*.txt"
        saveFileDialog.Title = "选择导出文件位置"
        saveFileDialog.FileName = $"样式查找结果_{styleName}.txt"

        If saveFileDialog.ShowDialog() <> DialogResult.OK Then Return

        Try
            range.Find.Style = doc.Styles(styleName)
            Dim results As New List(Of String)

            With range.Find
                .Text = ""
                .Forward = True
                .Wrap = WdFindWrap.wdFindContinue

                While .Execute()
                    Dim pageNum As Integer = range.Information(WdInformation.wdActiveEndPageNumber)
                    Dim lineNum As Integer = range.Information(WdInformation.wdFirstCharacterLineNumber)
                    results.Add($"在第 {pageNum} 页，第 {lineNum} 行找到样式：{styleName}")
                End While
            End With

            ' 写入结果到文件
            If results.Count > 0 Then
                System.IO.File.WriteAllLines(saveFileDialog.FileName, results.ToArray())
                MessageBox.Show($"查找完成，共找到 {results.Count} 处匹配。结果已保存至：{saveFileDialog.FileName}")
            Else
                MessageBox.Show("未找到指定样式的内容")
            End If

        Catch ex As Exception
            MessageBox.Show("查找过程中发生错误：" & ex.Message)
        End Try
    End Sub
End Class